<?=$header?>

<div class="container">
    <div class="installation_welcome">Welcome to SyteMonitor.  Please complete this form to complete installation.</div>
    <div class="installation_box">
        <div class="title">Database Configuration</div>
        <form name="frmInstall" method="post" action="/index.php/install">
            <table class="database_config" border="0" cellspacing="10">
                <tr><td class="label">Host</td><td><input class="txt" type="text" id="txtHost" name="txtHost"></td></tr>
                <tr><td class="label">Database User</td><td><input class="txt" type="text" id="txtUser" name="txtUser"></td></tr>
                <tr><td class="label">Database Password</td><td><input class="txt" type="text" id="txtPassword" name="txtPassword"></td></tr>
                <tr><td class="label">Database Name</td><td><input class="txt" type="text" id="txtDatabaseName" name="txtDatabaseName"></td></tr>
                <tr><td></td><td><input type="button" id="btnSubmit" value="submit"></td></tr>
            </table>
        </form>

        <?if(!$folder_writable){
            ?><div class="note">Note: The following folders are not writeable.  Please give them 0777 permission to continue installation:</div><br>
            <div><?=$_SERVER['DOCUMENT_ROOT']?>SMReceiver</div>
            <?
        }?>
    </div>
</div>

<script>
var SM = SM || {}
SM.Install = function(){
    var txtHost, txtUser, txtPassword, txtDatabaseName, btnSubmit, errors=[];

    var setVars = function(){
        txtHost = YAHOO.util.Dom.get("txtHost");
        txtUser = YAHOO.util.Dom.get("txtUser");
        txtPassword = YAHOO.util.Dom.get("txtPassword");
        txtDatabaseName = YAHOO.util.Dom.get("txtDatabaseName");
        btnSubmit = YAHOO.util.Dom.get("btnSubmit");
    }

    var validate = function(){
        errors = [];
        if(txtHost.value.length == 0) {
            errors.push("Please enter a host to continue.");
        }

        if(txtUser.value.length == 0) {
            errors.push("Please enter a user to continue.");
        }

        if(txtPassword.value.length == 0) {
            errors.push("Please enter a password to continue.");
        }

        if(txtDatabaseName.value.length == 0) {
            errors.push("Please enter a database name to continue.");
        }
    }

    var testConnectionTransaction = function(result){
        if(result.feedback == "success"){
            //submit form
            document.frmInstall.submit();
        }else{
            alert("Could not connect to database.  Please check your configuration settings.");
        }
    }

    var handleSubmitClick = function(){
        validate();
        if(errors.length > 0){
            var message = "The following errors have occurred:\n\n";
            for(var i=0; i < errors.length; i++){
                message += errors[i] + "\n";
            }
            
            alert(message);
        }else{
            // test for accurate db connection parameters
            var sUrl = "/index.php/install/testconnection";
            var postQueryString = "host=" + txtHost.value + "&user=" + txtUser.value + "&password=" + txtPassword.value + "&dbname=" + txtDatabaseName.value;
            SM.Transaction.registerCallback(testConnectionTransaction);
            SM.Transaction.doPost(sUrl, postQueryString);
        }

    }

    var bindEvents = function(){
        YAHOO.util.Event.addListener(btnSubmit, "click", handleSubmitClick);
    }

    var init = function(){
        setVars();
        bindEvents();
    }

    return {
        init:init
    }
}();
SM.YUILoader.registerCallback(SM.Install.init);
</script>

<?=$footer?>